/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Hangman;

import java.io.IOException;
import java.io.PrintWriter;
import java.security.Principal;
import javax.ejb.EJB;
import javax.naming.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.omg.CosNaming.NamingContextExtHelper;

/**
 *
 * @author stuartsealfon
 */
@WebServlet(name="gameBoard2", urlPatterns={"/gameBoard2"})
public class gameBoard2 extends HttpServlet {
    static int timer = 80;
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            out.println("<html>");
            out.println("<head>");
            out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
            out.println("<title>Game Board</title>");
            out.println("<style type=\"text/css\"> body { margin:0; } #wrap { width:600px; height:370px;");
            out.println("background-color:#7BECCA; position:absolute; top:20%; left:50%; margin-top:-50px;");
            out.println("margin-left:-250px; } </style>");
            out.println("</head>");
            out.println("<body onload=\"setTimeout('document.refreshPage2.submit()',2000);\">");

            out.println("<form action=\"index.jsp\" method=\"post\">"
                        + "<input type=\"hidden\" name=\"name\" value=" + request.getParameter("name")
                        + "><input type=\"hidden\" name=\"word\" value=" + request.getParameter("word")
                        + "><input type=\"submit\" value=\"Exit\" name=\"logout\"> </form> ");

            out.println("<form name=\"refreshPage2\" action=\"gameBoard2\""
                    + "ID=\"refreshPage2\" method=\"post\" name=\"test\"> "
                    + "<input type=\"hidden\" name=\"name\" value=" + request.getParameter("name")
                    + "><input type=\"hidden\" name=\"word\" value=" + request.getParameter("word")
                    + "><input type=\"hidden\" name=\"wait\" value=" + request.getParameter("name")
                    + "></form>");

            if (request.getParameter("wait") != null && !request.getParameter("wait").equals("null")) {
                timer-=1;
                if (timer < 0) timer = 80;
            }

            HangmanBean hangmanBean = new HangmanBean();
            if (request.getParameter("guess") != null && !request.getParameter("guess").equals("null")) {
                if (request.getParameter("guessLetter").length() > 0) {
                    hangmanBean.guessLetter(request.getParameter("guessLetter").charAt(0));
                    timer = timer/20;
                    timer = timer*20;
                }
            }
            boolean inGame = false;
            int playerNumber = -1;
            if ((request.getParameter("name") != null && !request.getParameter("name").equals("null"))
                    || (request.getParameter("word") != null && !request.getParameter("word").equals("null")))
                playerNumber = hangmanBean.addPlayerToGame(request.getParameter("name"), request.getParameter("word"));

            if (playerNumber == -1) out.println("<center>System does not allow this User screen to play the game.</center><br><br>");
            else {
            out.println("<div id=\"wrap\" align=\"center\"><br>");
            out.println("<form  method=\"post\"><table border=\"1\" >");
            for (int i = 0; i < 4; i++) {
            out.println("<tr><td>");
            if (hangmanBean.getPlayerName(i) != null && !hangmanBean.getPlayerName(i).equals("null"))
                out.println(hangmanBean.getPlayerName(i));
            out.println("</td>");
            for (int j = 0; j < 8; j++) {
                int cell = (i*8)+j;
                out.println("<td>");
                out.println(hangmanBean.getLetter((i*8)+j));
                out.println("</td>");
            }
            out.println("</tr>");
            out.println("<BR>");
            }
            out.println("</table><br><br>");

            //out.println(timer);

            int playersRemaining = 4;
            if (hangmanBean.gameFull()) {
            for (int i=0; i<4; i++) {
                if (hangmanBean.isEliminated(i)) {
                    out.println(hangmanBean.getPlayerName(i) + " is eliminated" + "<br>");
                    playersRemaining--;
                }
                else if (hangmanBean.getPlayerName(i) != null && !hangmanBean.getPlayerName(i).equals("null")) out.println(hangmanBean.getPlayerName(i) + " is still playing<br>");
            }
            if (playerNumber < 4 && hangmanBean.isEliminated(playerNumber))
                out.println(request.getParameter("name") + ", you are out!<br>");
            if (playersRemaining <= 1) out.println("We have a winner<br>");
                }

            if (timer/20 == playerNumber && hangmanBean.gameFull()) {
                if (hangmanBean.isEliminated(playerNumber)) {
                    timer /=20;
                    timer *=20;
                }
                    else {
                        out.println(request.getParameter("name") + ", it is your turn!  You have " + (timer/2)%10 + " seconds left.");

                        out.println("<input type=\"text\" name=\"guessLetter\" value=\"\" size=\"1\" />"
                            + "<input type=\"hidden\" name=\"name\" value=" + request.getParameter("name")
                            + "><input type=\"hidden\" name=\"word\" value=" + request.getParameter("word"));
                        out.println("><input type=\"hidden\" name=\"guess\" value=\"guessed\" size=\"1\" />");
                        out.println("<input type=\"submit\" value=\"Guess Letter\" name=\"guessLetter\" />");
                        out.println("<br><br>");
                        out.println("</form>");
                }
                }
            out.println("</div>");
                }

            out.println("</body>");
            out.println("</html>");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
